# coding:utf-8
import re

from config.confRead import Config


def mail_manage(ml):
    """
    email关联配置
    :param ml:
    :return:
    """
    try:
        relevance_list = re.findall(r"\${(.*?)}\$", ml)
        for n in relevance_list:
            pattern = re.compile(r'\${' + n + r'}\$')
            email_cf = Config()
            email_relevance = email_cf.read_email()
            ml = re.sub(pattern, email_relevance[n], ml, count=1)
    except TypeError:
        pass
    return ml


def dir_manage(directory):
    """
    directory关联配置
    :param directory:
    :return:
    """
    try:
        relevance_list = re.findall(r"\${(.*?)}\$", directory)
        for n in relevance_list:
            pattern = re.compile(r'\${' + n + r'}\$')
            dir_cf = Config()
            dir_relevance = dir_cf.read_dir()
            directory = re.sub(pattern, dir_relevance[n], directory, count=1)
    except TypeError:
        pass
    return directory


def db_manage(db):
    try:
        relevance_list = re.findall(r"\${(.*?)}\$", db)
        for n in relevance_list:
            pattern = re.compile(r'\${' + n + r'}\$')
            dir_cf = Config()
            dir_relevance = dir_cf.read_db()
            db = re.sub(pattern, dir_relevance[n], db, count=1)
    except TypeError:
        pass
    return db


if __name__ == '__main__':
    dirs = dir_manage(directory='${case_dir}$')
    mls = mail_manage(ml='${mail_pass}$')
    print(mls)
